package cc.ucanuup.service.base.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import cc.ucanuup.common.support.easyui.EasyuiUtil;
import cc.ucanuup.common.support.easyui.inter.EasyuiQuery;
import cc.ucanuup.common.support.easyui.model.SearchComgrid;
import cc.ucanuup.common.support.easyui.view.EasyuiTree;
import cc.ucanuup.service.base.dao.UpRoleJPA;
import cc.ucanuup.service.base.entity.UpRole;
import cc.ucanuup.service.base.inter.MasterCodeService;
import cc.ucanuup.service.base.inter.UpRoleService;


/**
 * Description: TODO[描述该类概要功能介绍]
 *
 * @author guwenbin
 * @date 2018年2月5日
 * @version 1.0
 */
@Service
public class UpRoleServiceImpl implements UpRoleService {

	@Autowired
	private UpRoleJPA upRoleJPA;
	@Autowired
	private MasterCodeService masterCodeService;

	@Override
	public List<EasyuiTree> findRoleTreeListByUserId(String userId) {
		List<UpRole> roleList = upRoleJPA.findRoleListByUserId(userId);
		List<EasyuiTree> rt = new ArrayList<EasyuiTree>();
		EasyuiTree tree = null;
		for(UpRole role : roleList){
			tree = new EasyuiTree();
			tree.setId(role.getId());
			tree.setText(role.getRoleName());
			rt.add(tree);
		}
		return rt;
	}

	@Override
	public UpRole findOne(String id) {

		return upRoleJPA.findOne(id);
	}

	@Override
	public Page<UpRole> queryRolesAndSortIt(EasyuiQuery bzPage) throws Exception {

		final Map<String, Object>  map = EasyuiUtil.castQueryParam(bzPage);
		String [] sorts = bzPage.getSortArr();
		String [] oders = bzPage.getOrders();
		List<Order> orders = new  ArrayList<Order>();
		Sort sort = null;
		if(sorts!=null && sorts.length>0){
			Order od = null;
			for(int i=0;i<sorts.length;i++){
				if("asc".equals(oders[i])){
					od = new Order(Direction.ASC,sorts[i]);
				}else{
					od = new Order(Direction.DESC,sorts[i]);
				}
				orders.add(od);
			}
			sort=new Sort(orders);
		}

		PageRequest pageable = new PageRequest(bzPage.getPageNo(), bzPage.getPageSize(), sort);
		Specification<UpRole> specification = new Specification<UpRole>() {

			@Override
			public Predicate toPredicate(Root<UpRole> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				List<Predicate> list = new ArrayList<Predicate>();
				Predicate roleName = cb.like(root.get("roleName").as(String.class),"%"+(String)map.get("roleName")+"%");
				Predicate deleted = cb.equal(root.get("deleted").as(Integer.class),0);
				list.add(roleName);
				list.add(deleted);
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		};
		Page<UpRole> sourceCodes= upRoleJPA.findAll(specification, pageable);
		return sourceCodes;
	}

	@Override
	public UpRole save(UpRole role) {

		return upRoleJPA.save(role);
	}

	@Override
	public List<UpRole> findDefaultRoleWithType(String userType) {
		if(null != userType){
			if(userType.equals("0")){
				return upRoleJPA.findDefaultRoleWithType("default_sp");
			}
			if(userType.equals("1")){
				return upRoleJPA.findDefaultRoleWithType("default_user");
			}
		}
		return null;
	}

	@Override
	public Page<UpRole> queryRolesByRoleName(final SearchComgrid comgrid) {
		Order od = new Order(Direction.ASC,"roleName");
		Sort sort = new Sort(od);
		PageRequest pageable = new PageRequest(comgrid.getPageNo(), comgrid.getPageSize(), sort);
		Specification<UpRole> specification = new Specification<UpRole>() {

			@Override
			public Predicate toPredicate(Root<UpRole> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				Predicate roleName = cb.like(root.get("roleName").as(String.class),"%"+comgrid.getQ() == null? "" :comgrid.getQ()+"%");
				list.add(roleName);
				Predicate type1 = cb.equal(root.get("powerType").as(String.class),"user");
				//Predicate type2 = cb.equal(root.get("powerType").as(String.class),"user");
				list.add(type1 /*cb.or(type1,type2)*/);
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}

		};
		Page<UpRole> sourceCodes= upRoleJPA.findAll(specification, pageable);
		return sourceCodes;
	}

}
